import dayjs from "dayjs";
import { message } from "@/utils/message";
import { getOnlineLogsList } from "@/api/system";
import { reactive, ref, onMounted, toRaw } from "vue";
import type { PaginationProps } from "@pureadmin/table";
import { getOnlineUsers, forceLogout } from "@/api/system";
import { Console } from "console";
import {
  subscribeToReceiveMessage,
  unsubscribeFromMessage,
  getMessageType,
  MessageTemplate,
  MessageType
} from "@/utils/signalRService";

export function useRole() {
  const form = reactive({
    username: ""
  });
  const dataList = ref([]);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    {
      label: "序号",
      prop: "connectionId",
      minWidth: 60
    },
    {
      label: "用户名",
      prop: "username",
      minWidth: 100
    },
    {
      label: "登录 IP",
      prop: "ipAddress",
      minWidth: 140
    },
    {
      label: "登录地点",
      prop: "address",
      minWidth: 140
    },
    {
      label: "操作系统",
      prop: "os",
      minWidth: 100
    },
    {
      label: "浏览器类型",
      prop: "browser",
      minWidth: 100
    },
    {
      label: "登录时间",
      prop: "time",
      minWidth: 180,
      formatter: ({ time }) => dayjs(time).format("YYYY-MM-DD HH:mm:ss")
    },
    {
      label: "操作",
      fixed: "right",
      slot: "operation"
    }
  ];

  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
  }

  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
  }

  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }

  async function handleOffline(row) {
    await forceLogout({ username: row.username });
  }

  // 定义处理接收到消息的回调函数
  const handleReceiveNotification = (message: string) => {
    const msg = JSON.parse(message) as MessageTemplate;
    if (getMessageType(msg.messageType.toString()) === MessageType.Online) {
      onSearch();
    }
    if (getMessageType(msg.messageType.toString()) === MessageType.Offline) {
      setTimeout(() => {
        onSearch();
      }, 3000);
    }
  };

  async function onSearch() {
    loading.value = false;
    const { data } = await getOnlineUsers(toRaw(form));
    dataList.value = data;
    // pagination.total = data.totalItems;
    // //pagination.pageSize = data.pageSize;
    // pagination.currentPage = data.currentPage;

    loading.value = false;
  }

  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  };

  onMounted(() => {
    unsubscribeFromMessage("ReceiveNotification", handleReceiveNotification);
    subscribeToReceiveMessage("ReceiveNotification", handleReceiveNotification);
    onSearch();
  });

  return {
    form,
    loading,
    columns,
    dataList,
    pagination,
    onSearch,
    resetForm,
    handleOffline,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange
  };
}
